package com.ysd.handler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.LocalDateTime;

@Component
public class MetaHandler extends BaseController implements MetaObjectHandler {

    private static final Logger logger = LoggerFactory.getLogger(MetaHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        String userId = "";
        String orgId = "";
        try {
            userId = getNowLoadPeople();
        } catch (Exception e) {
        }
        try {
            System.out.println(JSONUtil.toJsonStr(metaObject));
            JSONObject jsonObject = JSONUtil.parseObj(metaObject);
            System.out.println(jsonObject.get("originalObject"));
            JSONObject originalObject = JSONUtil.parseObj(jsonObject.get("originalObject"));
            orgId = originalObject.getStr("interfaceOrg");
            if (StringUtils.isEmpty(orgId)) {
                orgId = originalObject.getStr("orgId");
            }

            if (StringUtils.isEmpty(userId)) {
                userId = originalObject.getStr("userId");
            }
            System.out.println("入参组织机构为" + orgId);
        } catch (Exception e) {
            System.out.println("获取组织机构失败");
        }
        if(ToolsUtils.isEmpty(orgId)){
            try {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                String token = request.getHeader("Authorization").replace("Bearer ", "");
                orgId = RedisUserManager.getStoreOrg("currentOrg" + token);
                System.out.println("缓存中的组织机构为" + orgId);
            } catch (Exception e) {
                System.out.println("缓存中获取组织机构失败");
            }
        }


        this.setFieldValByName("createTime", Timestamp.valueOf(LocalDateTime.now()), metaObject);
        this.setFieldValByName("creator", userId, metaObject);
        this.setFieldValByName("editTime", Timestamp.valueOf(LocalDateTime.now()), metaObject);
        this.setFieldValByName("editor", userId, metaObject);
        this.setFieldValByName("delFlag", 0, metaObject);
        this.setFieldValByName("orgId", orgId, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userId = "";
        try {
            userId = getNowLoadPeople();
        } catch (Exception e) {
        }
        this.setFieldValByName("editTime", Timestamp.valueOf(LocalDateTime.now()), metaObject);
        this.setFieldValByName("editor", userId, metaObject);
    }
}
